home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / mod2tutb.zip / BAKRSTR.MOD < prev    next >
Text File  |  1989-01-18  |  7KB  |  227 lines

  1. MODULE BakRstr;
  2.  
  3. (* This program is used to restore the files from the floppy disks *)
  4. (* to the hard disk.  This program is loaded into the root direc-  *)
  5. (* tory of the hard disk and executed from there.  The files are   *)
  6. (* read from the floppy and copied into the same directory of the  *)
  7. (* hard disk as they are in on the floppy, the directories being   *)
  8. (* created as needed on the hard disk.  To restore additional      *)
  9. (* disks, simply rerun this program once for each disk.            *)
  10. (*                                                                 *)
  11. (*            Copywrite 1987, 1989 - Coronado Enterprises          *)
  12.  
  13. FROM InOut         IMPORT WriteString,Read,Write,WriteLn;
  14. FROM DiskDirectory IMPORT CurrentDrive,CurrentDirectory;
  15. FROM Strings       IMPORT Concat,Copy,Insert,Delete,Length,
  16.                           CompareStr;
  17. FROM Storage       IMPORT ALLOCATE,DEALLOCATE;
  18. FROM FileSystem    IMPORT File;
  19. FROM SYSTEM        IMPORT ADDRESS,TSIZE,SIZE;
  20. FROM DirHelps      IMPORT ReadFileStats,FileDataPointer,FileData,
  21.                           CopyFile,ChangeToDirectory;
  22.  
  23. VAR SourceDrive  : CHAR;
  24.     TargetDrive  : CHAR;
  25.     StartingPath : ARRAY[0..64] OF CHAR;
  26.     FileList     : File;
  27.     DiskTransAdr : ARRAY[1..43] OF CHAR;
  28.  
  29.  
  30.  
  31.  
  32. PROCEDURE Initialize();
  33. VAR StorageFile : ARRAY[0..20] OF CHAR;
  34. BEGIN
  35.    WriteString("Enter the source drive, one letter ---> ");
  36.    Read(SourceDrive);
  37.    SourceDrive := CAP(SourceDrive);
  38.    Write(SourceDrive);
  39.    WriteLn;
  40.    CurrentDrive(TargetDrive);
  41.    TargetDrive := 'A';
  42.    Copy("A:",0,64,StartingPath);
  43.    StartingPath[0] := SourceDrive;
  44. END Initialize;
  45.  
  46.  
  47.  
  48. (* This procedure is used to copy the files from the floppy to the *)
  49. (* hard disk while making a list of subdirectories found in order  *)
  50. (* to copy each of them also.                                      *)
  51.  
  52. PROCEDURE StoreData(NewData         : FileDataPointer;
  53.                     VAR Directories : FileDataPointer);
  54.      PROCEDURE AddToTree(VAR RootOfTree : FileDataPointer;
  55.                          VAR NewNode    : FileDataPointer);
  56.      VAR Result : INTEGER;
  57.      BEGIN
  58.         Result := CompareStr(RootOfTree^.Name,NewNode^.Name);
  59.         IF Result = 1 THEN
  60.            IF RootOfTree^.Left = NIL THEN
  61.               RootOfTree^.Left := NewNode;
  62.            ELSE
  63.               AddToTree(RootOfTree^.Left,NewNode);
  64.            END;
  65.         ELSE
  66.            IF RootOfTree^.Right = NIL THEN
  67.               RootOfTree^.Right := NewNode;
  68.            ELSE
  69.               AddToTree(RootOfTree^.Right,NewNode);
  70.            END;
  71.         END;
  72.      END AddToTree;
  73.  
  74. VAR  Error      : CARDINAL;
  75.      SourceFile : ARRAY[0..20] OF CHAR;
  76.      DestFile   : ARRAY[0..20] OF CHAR;
  77. BEGIN
  78.    IF NewData^.Attr = 010H THEN         (* Attr = 10 for directory *)
  79.       IF NewData^.Name[0] <> "." THEN
  80.          IF Directories = NIL THEN
  81.             Directories := NewData;
  82.          ELSE
  83.             AddToTree(Directories,NewData);
  84.          END;
  85.       END;
  86.    ELSE                                    (* Otherwise a filename *)
  87.       WriteString("Copyfile ---> ");
  88.       WriteString(NewData^.Name);
  89.       WriteLn;
  90.       Copy(NewData^.Name,0,20,SourceFile);
  91.       Insert(SourceDrive,SourceFile,0);
  92.       Insert(':',SourceFile,1);
  93.       Copy(NewData^.Name,0,20,DestFile);
  94.       Insert(TargetDrive,DestFile,0);
  95.       Insert(':',DestFile,1);
  96.       CopyFile(SourceFile,DestFile,NewData^.Size,Error);
  97.       IF Error <> 0 THEN
  98.          WriteString("Error copying file ---> ");
  99.          WriteString(SourceFile);
  100.          WriteLn;
  101.       END;
  102.    END;
  103. END StoreData;
  104.  
  105.  
  106.  
  107. (* This procedure reads the file statistics from DOS and stores    *)
  108. (* the data in a record for further use.                           *)
  109.  
  110. PROCEDURE ReadFileStatistics(VAR Directories : FileDataPointer;
  111.                              PathToFiles : ARRAY OF CHAR);
  112. TYPE MaskStore = ARRAY[0..70] OF CHAR;
  113. VAR MaskAndFile  : MaskStore;         (* Used for file search name *)
  114.     ModifiedPath : MaskStore;
  115.     MaskAddr     : ADDRESS;
  116.     Error        : BOOLEAN;
  117.     Index        : CARDINAL;
  118.     NewData      : FileDataPointer;
  119.     FirstFile    : BOOLEAN;
  120. BEGIN
  121.    WriteString("Changepath ---> ");
  122.    WriteString(PathToFiles);
  123.    WriteLn;
  124.    Copy(PathToFiles,0,64,ModifiedPath);
  125.    IF ModifiedPath[2] = 000C THEN
  126.       ModifiedPath[2] := '\';
  127.       ModifiedPath[3] := 000C;
  128.    END;
  129.    ModifiedPath[0] := TargetDrive;
  130.    ChangeToDirectory(ModifiedPath,TRUE,Error);
  131.    IF Error THEN
  132.       WriteString("Cannot change target directory ---> ");
  133.       WriteString(ModifiedPath);
  134.       WriteLn;
  135.    END;
  136.    ModifiedPath[0] := SourceDrive;
  137.    ChangeToDirectory(ModifiedPath,FALSE,Error);
  138.    IF Error THEN
  139.       WriteString("Cannot change source directory ---> ");
  140.       WriteString(ModifiedPath);
  141.       WriteLn;
  142.    END;
  143.    ALLOCATE(NewData,TSIZE(FileData));
  144.    FirstFile := TRUE;
  145.    Concat(PathToFiles,"/*.*",MaskAndFile);
  146.    ReadFileStats(MaskAndFile,FirstFile,NewData,Error);
  147.    IF NOT Error THEN
  148.       StoreData(NewData,Directories);
  149.    END;
  150.  
  151.    REPEAT
  152.       ALLOCATE(NewData,TSIZE(FileData));
  153.       FirstFile := FALSE;
  154.       ReadFileStats(MaskAndFile,FirstFile,NewData,Error);
  155.       IF NOT Error THEN
  156.          StoreData(NewData,Directories);
  157.       END;
  158.    UNTIL Error;
  159.  
  160. END ReadFileStatistics;
  161.  
  162.  
  163.  
  164. (* This procedure searches all subdirectory names found in a       *)
  165. (* search of a subdirectory for additional files and subdirector-  *)
  166. (* ies.  The search is recursive.                                  *)
  167.  
  168. PROCEDURE DoAllSubdirectories(StartPath : ARRAY OF CHAR;
  169.                               Directories : FileDataPointer);
  170. VAR NewPath : ARRAY[0..64] OF CHAR;
  171.     Index   : CARDINAL;
  172. BEGIN
  173.    IF Directories <> NIL THEN
  174.       IF Directories^.Left <> NIL THEN
  175.          DoAllSubdirectories(StartPath,Directories^.Left);
  176.       END;
  177.       IF Directories^.Name[0] <> '.' THEN
  178.          Copy(StartPath,0,64,NewPath);
  179.          Insert('\',NewPath,Length(NewPath));
  180.          Concat(NewPath,Directories^.Name,NewPath);
  181.          FOR Index := (SIZE(NewPath)-1) TO 1 BY -1 DO
  182.             IF NewPath[Index] = ' ' THEN
  183.                NewPath[Index] := 000C;
  184.             END;
  185.          END;
  186.          GetAllFilesAndDirectories(NewPath);
  187.       END;
  188.       IF Directories^.Right <> NIL THEN
  189.          DoAllSubdirectories(StartPath,Directories^.Right);
  190.       END;
  191.    END;
  192. END DoAllSubdirectories;
  193.  
  194.  
  195.  
  196. (* This procedure deletes a tree after it is no longer needed.     *)
  197.  
  198. PROCEDURE DeleteTree(Point : FileDataPointer);
  199. BEGIN
  200.    IF Point <> NIL THEN
  201.       DeleteTree(Point^.Left);
  202.       DeleteTree(Point^.Right);
  203.       DEALLOCATE(Point,TSIZE(FileData));
  204.    END;
  205. END DeleteTree;
  206.  
  207.  
  208.  
  209. PROCEDURE GetAllFilesAndDirectories(ThisPath : ARRAY OF CHAR);
  210. VAR DirExists   : BOOLEAN;           (* Temporary - use logic later*)
  211.     Directories : FileDataPointer;   (* Point to root of Dir tree  *)
  212. BEGIN
  213.    Directories := NIL;
  214.    ReadFileStatistics(Directories,ThisPath);
  215.    DoAllSubdirectories(ThisPath,Directories);
  216.    DeleteTree(Directories);
  217. END GetAllFilesAndDirectories;
  218.  
  219.  
  220.  
  221.  
  222. BEGIN   (* Main program - BakRstr, Backup restore *)
  223.   Initialize;
  224.   GetAllFilesAndDirectories(StartingPath);
  225. END BakRstr.
  226.  
  227.